//
//  SNVisitorView.swift
//  sinaWeBoo
//
//  Created by Fly on 15/10/19.
//  Copyright © 2015年 Fly. All rights reserved.
//

import UIKit

let Margin: CGFloat = 16
let LabHeight:CGFloat = 35

class SNVisitorView: UIView {

    var homeIcon: UIImageView?
    var maskIcon: UIImageView?
    
    // setUpViewInfo
    func setupViewInfo(imageName: String,animImage: String?,message: String){

        // icon image
        iconView.image = UIImage(named: imageName)
        // label text
        messeageLabel.text = message
        
        // homeIcon image
        if let anim = animImage {
            homeIcon = UIImageView(image: UIImage(named: anim))
            maskIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        }
        setUpUI()
    }
    
    // 重写构造函数
    // init with frame 是视图的指定构造函数
    override init(frame: CGRect) {
        // super init 才可以创建view 对象
        super.init(frame: frame)
        
        // set up visitor view setting
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        // set up visitor view setting
    }
    
    // MARK: - add subView
    lazy var iconView: UIImageView = UIImageView()
    
    lazy var messeageLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.darkGrayColor()
        lab.font = UIFont.systemFontOfSize(14)
        lab.numberOfLines = 0
        lab.textAlignment = NSTextAlignment.Left
        lab.sizeToFit()
        
        return lab
    }()
    
    // login btn
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        return btn
        }()
    
    // register btn
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        return btn
    }()
}

// MARK: - set up UI
extension SNVisitorView{
    // set up ui
    func setUpUI(){
        
        // add subViews
        addSubview(iconView)
        
        if let home = homeIcon{
            if let mask = maskIcon {
                addSubview(home)
                
                addSubview(mask)
            }
        }
        
        addSubview(messeageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // layout the subviews 
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 1.iconView: <-> self(view) constraint centerX = centerY
        addConstraint(NSLayoutConstraint(item: iconView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: -60))
        
        // 2.homeIcon: <-> iconView constraint centerX = centerY
        if let home = homeIcon{
            addConstraint(NSLayoutConstraint(item: home,
                attribute: .CenterX,
                relatedBy: .Equal,
                toItem: iconView,
                attribute: .CenterX,
                multiplier: 1.0,
                constant: 0))
            addConstraint(NSLayoutConstraint(item: home,
                attribute: .CenterY,
                relatedBy: .Equal,
                toItem: iconView,
                attribute: .CenterY,
                multiplier: 1.0,
                constant: 0))
        }
        
        // 3.messageView: <-> iconView constraint top = bottom
        addConstraint(NSLayoutConstraint(item: messeageLabel,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: iconView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: messeageLabel,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: iconView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: Margin))
        addConstraint(NSLayoutConstraint(item: messeageLabel,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 224))
        addConstraint(NSLayoutConstraint(item: messeageLabel,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 35))
        
        // 4.loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Left,
            relatedBy: .Equal,
            toItem: messeageLabel,
            attribute: .Left,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: messeageLabel,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: Margin))
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 35))
        
        // 5.registerBtn
        addConstraint(NSLayoutConstraint(item: registerBtn,
            attribute: .Right,
            relatedBy: .Equal,
            toItem: messeageLabel,
            attribute: .Right,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: messeageLabel,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: Margin))
        addConstraint(NSLayoutConstraint(item: registerBtn,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 35))

        // self <-> 6.maskView: <-> messageLabel constraint
        if let mask = maskIcon{
            addConstraint(NSLayoutConstraint(item: mask,
                attribute: .Top,
                relatedBy: .Equal,
                toItem: self,
                attribute: .Top,
                multiplier: 1.0,
                constant: 0))
            addConstraint(NSLayoutConstraint(item: mask,
                attribute: .Bottom,
                relatedBy: .Equal,
                toItem: loginBtn,
                attribute: .Bottom,
                multiplier: 1.0,
                constant: 0))
            addConstraint(NSLayoutConstraint(item: mask,
                attribute: .Width,
                relatedBy: .Equal,
                toItem: self,
                attribute: .Width,
                multiplier: 1.0,
                constant: 0))
        }
        
        // 设置backColor
        backgroundColor = UIColor(white: 237.0/256.0, alpha: 1)
    }
}
